package part18;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author wcj
 * @Date 2021/6/4 16:28
 * @Version 1.0
 */
public class Part1827 {

    static int length = 0x8FFFFFF;

    /**
     * 使用MappedByteBuffer进行映射文件的访问
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        MappedByteBuffer map = new RandomAccessFile("test.dat", "rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
        for (int i = 0; i < length; i++) {
            map.put((byte) 'x');
        }
        System.out.println("finished writing");
        for (int i = length / 2; i < length / 2 + 6; i++) {
            System.out.println((char) map.get());
        }
    }
}
